import { useI18n } from "@/hooks/useI18n";

const { t } = useI18n();
export const formRules = {
  // 校验用户名
  validUserName: (_: any, value: string, callback: any) => {
    if (!value.trim()) {
      callback(new Error(t("validators.username.cannotBeEmpty")));
    } else {
      callback();
    }
  },
  // 校验密码
  validPwd: (_: any, value: string, callback: any) => {
    const re = new RegExp(/^[0-9a-zA-Z]*$/);
    if (!value.trim()) {
      callback(new Error(t("validators.validPwd.cannotBeEmpty")));
    } else if (value.trim().length < 6) {
      callback(new Error(t("validators.validPwd.atLeastSixDigits")));
    } else if (value.trim().length > 20) {
      callback(new Error(t("validators.validPwd.upToTwentyDigits")));
    } else if (!re.test(value.trim())) {
      callback(new Error(t("validators.validPwd.lettersOrnumbers")));
    } else {
      callback();
    }
  },
  // 校验验证码
  validCode: (_: any, value: string, callback: any) => {
    if (!value.trim()) {
      callback(new Error(t("validators.code.cannotBeEmpty")));
    } else {
      callback();
    }
  },
  checkRoleLength: (_: any, value: any[], callback: any) => {
    if (value.length) {
      callback();
    } else {
      callback(new Error(t("validators.checkRoleLength.cannotBeEmpty")));
    }
  },
  // 校验邮箱
  email: (_: any, value: string, callback: any) => {
    const reg = new RegExp(
      /^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+((.[a-zA-Z0-9_-]{2,3}){1,2})$/
    );
    if (value && !reg.test(value)) {
      callback(new Error(t("validators.email.illegalEmail")));
    } else {
      callback();
    }
  },
  // 校验手机号码
  moblie: (_: any, value: string, callback: any) => {
    const reg = new RegExp(/^1[34578]\d{9}$/);
    if (value && !reg.test(value)) {
      callback(new Error(t("validators.moblie.illegalMoblie")));
    } else {
      callback();
    }
  },
  // 是否是数字
  number: (_: any, value: string, callback: any) => {
    const reg = new RegExp(/^[0-9]*$/);
    if (!reg.test(value)) {
      callback(new Error(t("validators.number.illegalNumber")));
    } else {
      callback();
    }
  },
  // 正整数
  positiveNumber: (_: any, value: string, callback: any) => {
    const reg = new RegExp(/^[1-9]\d*$/);
    if (!value.replace(/^(\s*)|(\s*)$/g, "")) {
      callback(new Error(t("validators.positiveNumber.cannotBeEmpty")));
    } else if (!reg.test(value)) {
      callback(new Error(t("validators.positiveNumber.positiveInteger")));
    } else {
      callback();
    }
  },
  // 校验字符串，排除空格情况
  str: (_: any, value: string, callback: any) => {
    if (!value.replace(new RegExp(/^(\s*)|(\s*)$/g), "")) {
      callback(new Error(t("validators.str.cannotBeEmpty")));
    } else {
      callback();
    }
  },
  // 用户名校验
  username: (_: any, value: string, callback: any) => {
    const reg = new RegExp(/^[\u4e00-\u9fa5_a-zA-Z0-9.·]+$/);
    if (!reg.test(value)) {
      callback(new Error(t("validators.username.illegalUsername")));
    } else {
      callback();
    }
  },
  time: (_: any, value: string, callback: any) => {
    const nowTime = new Date().getTime();
    const currentTime = new Date(value).getTime();
    if (!value) {
      callback(new Error(t("validators.time.cannotBeEmpty")));
    }
    if (currentTime < nowTime) {
      callback(new Error(t("validators.time.pastTime")));
    } else {
      callback();
    }
  },
  IdCard: (_: any, value: string, callback: any) => {
    // 身份证号码为15位或者18位，15位时全为数字，18位前17位为数字，最后一位是校验位，可能为数字或字符X
    const reg = new RegExp(/(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/);
    if (!value) {
      callback(new Error(t("validators.IdCard.cannotBeEmpty")));
    }
    if (reg.test(value) === false) {
      callback(new Error(t("validators.IdCard.illegalIdCard")));
    } else {
      callback();
    }
  },
};
